package rule.rulechain;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by fengxiangxiang on 2016-10-14.
 */
public class UpdateFile implements TestRule {
    public UpdateFile(String content, String file) {
        File file1 = null;
        FileOutputStream fop = null;
        try {
            file1 = new File(file);
            if (file1.exists()) {
                fop = new FileOutputStream(file1);
                byte[] contentInBytes = content.getBytes();
                fop.write(contentInBytes);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fop.flush();
            fop.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("starting updating file");
    }

    @Override
    public Statement apply(final Statement statement,final Description description) {
        System.out.println("Step3:New file is updated");
        return statement;
    }
}
